﻿using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
using BCEntity.TMS.Driver.DriverLearnDatum;
using BCCommon;
using System.Threading.Tasks;

namespace BCData.TMS.Driver.DriverLearnDatum
{
    public class DriverLearnDatumAnswerData : IDriverLearnDatumAnswerData
    {
        private const string QUERYSQL = "SELECT DatumAnswerId,AnswerContent,CreateTime,ModifyAdminId,ModifyDateTime FROM  DriverLearnDatumAnswer ";

        private readonly IDatabaseContext databaseContext;

        public DriverLearnDatumAnswerData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        /// <summary>
        /// 新增司机学习资料答案
        /// </summary>
        /// <param name="entity">司机学习资料答案</param>
        /// <returns></returns>
        public async Task<int> InsertAsync(DriverLearnDatumAnswerEntity entity)
        {
            string sql = @"INSERT INTO DriverLearnDatumAnswer(AnswerContent,CreateTime,ModifyAdminId,ModifyDateTime) VALUES(?AnswerContent,?CreateTime,?ModifyAdminId,?ModifyDateTime); SELECT LAST_INSERT_ID();";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CreateTime", DateTime.Now, DbType.DateTime);
            parameter.Add("?AnswerContent", entity.AnswerContent, DbType.AnsiString, size: 200);
            parameter.Add("?ModifyAdminId", entity.ModifyAdminId, DbType.Int64);
            parameter.Add("?ModifyDateTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.ExecuteScalarAsync<int>(sql, parameter);
        }

        /// <summary>
        /// 检查学习资料答案是否存在
        /// </summary>
        /// <param name="DatumAnswerId">学习资料答案Id</param>
        /// <returns></returns>
        public async Task<bool> CheckExistsByIdAsync(long DatumAnswerId)
        {
            string sql = @"SELECT 1 FROM DriverLearnDatumAnswer WHERE DatumAnswerId=?DatumAnswerId LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?DatumAnswerId", DatumAnswerId, DbType.Int64);
            var result = await this.databaseContext.Master.ExecuteScalarAsync(sql, parameter);
            return result != null;
        }

        /// <summary>
        /// 删除司机学习资料答案
        /// </summary>
        /// <param name="DatumAnswerId">学习资料答案Id</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int DatumAnswerId)
        {
            string sql = @"DELETE FROM DriverLearnDatumAnswer WHERE DatumAnswerId=?DatumAnswerId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?DatumAnswerId", DatumAnswerId, DbType.Int64);
            var i = await this.databaseContext.Master.ExecuteAsync(sql, parameter);
            return i > 0;
        }

        /// <summary>
        /// 获取指定的一条学习资料答案
        /// </summary>
        /// <param name="DatumAnswerId">学习资料答案Id</param>
        /// <returns></returns>
        public async Task<DriverLearnDatumAnswerEntity> GetDriverLearnDatumAnswerByIdAsync(int DatumAnswerId)
        {
            string sql = string.Format(@"{0} WHERE DatumAnswerId=?DatumAnswerId", QUERYSQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?DatumAnswerId", DatumAnswerId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<DriverLearnDatumAnswerEntity>(sql, parameter);
        }

        // <summary>
        /// 分页查询司机学习资料答案
        /// </summary>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <param name="answerContent">学习资料答案内容</param>
        /// <returns></returns>
        public async Task<(IEnumerable<DriverLearnDatumAnswerEntity>, int)> GetDriverLearnDatumAnswersAsync(int pageIndex, int pageSize, string answerContent = null)
        {
            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            int startIndex = (pageIndex - 1) * pageSize;
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrEmpty(answerContent))
            {
                wheres.Add("AnswerContent = ?AnswerContent");
                parameters.Add("?AnswerContent", answerContent, DbType.AnsiString);
            }
            string sql = string.Format("{0} {1} LIMIT {2}, {3}; SELECT COUNT(1) FROM DriverLearnDatumAnswer {1}",
                QUERYSQL,
                wheres.Count > 0 ? "WHERE " + string.Join(" AND ", wheres) : string.Empty,
                startIndex,
                pageSize);

            return await this.databaseContext.QueryMultipleAsync<DriverLearnDatumAnswerEntity, int>(sql, parameters);
        }

        /// <summary>
        /// 修改司机学习资料答案
        /// </summary>
        /// <param name="entity">司机学习资料答案</param>
        /// <returns></returns>
        public async Task<DriverLearnDatumAnswerEntity> UpdateAsync(DriverLearnDatumAnswerEntity entity)
        {
            string updateSql = @"UPDATE DriverLearnDatumAnswer SET AnswerContent=?AnswerContent,ModifyAdminId=?ModifyAdminId,ModifyDateTime=?ModifyDateTime WHERE DatumAnswerId=?DatumAnswerId;";

            string sql = string.Format(@"{0} {1}WHERE DatumAnswerId=?DatumAnswerId", updateSql, QUERYSQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?AnswerContent", entity.AnswerContent, DbType.AnsiString, size: 200);
            parameter.Add("?ModifyAdminId", entity.ModifyAdminId, DbType.Int64);
            parameter.Add("?ModifyDateTime", DateTime.Now, DbType.DateTime);
            parameter.Add("?DatumAnswerId", entity.DatumAnswerId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<DriverLearnDatumAnswerEntity>(sql, parameter);
        }

        public async Task<int> DeleteByDatumAnswerIdsAsync(int[] datumAnswerIds)
        {
            string ids = string.Empty;
            if (datumAnswerIds != null && datumAnswerIds.Length > 0)
            {
                ids = string.Join(",", datumAnswerIds);
            }
            string sql = string.Format(@"DELETE from DriverLearnDatumAnswer WHERE  DatumAnswerId IN ({0})", string.Join(",", ids));
            var result = await this.databaseContext.Master.ExecuteScalarAsync<int>(sql, CommandType.Text);
            return result;
        }
    }
}
